/**
 * 诊断表行所对应的构造器
 * @param sname
 * @param symptoms
 * @param drugs
 * @constructor
 */
function RecipteItem(sname,symptoms, drugs,match) {
  this.sname = sname;
  this.symptoms = symptoms;//病症
  this.drugs = drugs;//药物
  this.match = match;//匹配数
}
function Drug(medicineName,price,composition,effect){
  this.medicineName = medicineName;
  this.price = price;
  this.composition = composition;
  this.effect = effect;
}
/*
表格是全局变量
因为表格中的drug只保存了药物的名字，所以需要单独一个变量保存药品的其他信息
初始化的代码放在新建tab的代码里
 */
var recipetb = new Array();
var drugstb =new Array();

function getsyp(){//获取病情表格的数据
  var data = $('#sdl').datalist('getChecked');
  var sys = new Array();
  var cnt = data.length;


  for(var i=0;i<cnt;i++){
    var snamo = data[i].group;
    var tabindex = snamo.indexOf(' ');
    var ssname = snamo.substr(tabindex+1,snamo.length);
    console.log(ssname);
    sys.push(ssname+":"+data[i].text);
  }
  return sys;
}
function accept(){
  if (endEditing()){
    $('#dg').datagrid('acceptChanges');
  }
}
function checkInput(medicineName,composition,effect,price) {
  if(medicineName=="") return false;
  if(composition == "") return false;
  if(effect=="") return false;
  if(price=="") return false;
  return true;
}

function reject(){
  console.log("enter reject");
  $('#dg').datagrid('reload',{
    medicineName: '',
    price: '',
    composition: '',
    effect: ''
  });
}

function getdrugs() {
  accept();//先要保存药物表的值
  var data= $('#dg').datagrid('getRows');//然后才能获取到
  var cnt = data.length;
  var drugs = new Array();//表格中要显示的药物

  for(var i=0;i<cnt;i++){

    var medicineName = data[i].medicineName;
    var price = data[i].price;
    var composition = data[i].composition;
    var effect = data[i].effect;

    var drug = new Drug(medicineName,price,composition,effect);

    drugs.push(medicineName);
    drugstb.push(drug);//需保存进数据库的的药物

  }
  return drugs;
}

function saveCurrentData() {
  var sys = getsyp();
  var ds = getdrugs();
  var sn = $('#ssname').textbox('getText');
  var match = $('#match').numberbox('getValue');//获取匹配度输入
  var ri = new RecipteItem(sn,sys, ds,match);

  recipetb.push(ri);
}


function createTable(){

  var recipetable = '<table border="1">';
  var tbtitle = '<caption>诊断表</caption>';
  var tbhead = '<tr><th>病名</th><th>病情项</th><th>药物</th><th>匹配度</th></tr>';
  recipetable += tbtitle;
  recipetable += tbhead;
  var rowcount = recipetb.length;//表格行数

  for(var r=0;r<rowcount;r++){
    var item = recipetb[r];
    var row='<tr>';
    var spylen = item.symptoms.length;//单行中病情的个数
    var drlen = item.drugs.length;//单行中药物的个数
    //病名
    var sntd='<td>';
    var sickname = item.sname;
    sntd += sickname;
    sntd+='</td>';
    row+=sntd;
    //病情项
    var syptd='<td>';
    for (var i = 0; i < spylen; i++) {
      syptd += item.symptoms[i];
      syptd +=  ';';
    }
    syptd+='</td>';
    row+=syptd;
    //药品
    var drugtd = '<td>';
    for (var i = 0; i < drlen; i++) {
      drugtd += item.drugs[i];
      drugtd +=  ';';
    }
    drugtd+='</td>';
    row+=drugtd;
    //匹配
    var matchtd='<td>';
    matchtd+=item.match;
    matchtd+='</td>';
    row+=matchtd;

    row+='</tr>';
    recipetable+=row;
  }
  recipetable += '</table>';//根据输入拼接成了一个表格
  $('#infowin').html(recipetable);

}
function showWindow(){
  createTable();
  $('#infowin').window('open');
  $('#infowin').window('center');
}

//点击确认后保存当前数据
function confirmCommit() {
  $.messager.confirm('确认保存', '将产生一条新的对应关系数据\n提交后本条关系将无法修改',saveCurrentData());
}